package info.decamps.m2settings;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.ValidationException;

public class SettingsXMLFileBinder extends SettingsXMLBinder {
	private File settingsFile;
	/**
         * Constructs a settings binder to the config file specified by settingsDotXml
         **/
	public SettingsXMLFileBinder(File settingsDotXML) {
		settingsFile=settingsDotXML;
	}
	
        /**
         *Constructs a settings binder using the default config file.
         *It settings.xml does not exist, it will be created.
         */
        public SettingsXMLFileBinder() throws FileNotFoundException, MarshalException, ValidationException {
            this(getDefaultSettingsDotXml());
        }
        
	public void loadSettings() throws FileNotFoundException, MarshalException, ValidationException {
		FileReader fr=new FileReader(settingsFile);
		readSettings(fr);
	}
	
	public void saveSettings() throws IOException, MarshalException, ValidationException {
		FileWriter fw=new FileWriter(settingsFile);
		writeSettings(fw);
	}
        
        public static File getDefaultSettingsDotXml() {
            return new File(System.getProperty("user.home")+"/.m2/settings.xml");
        }
}